<?php require_once('../Connections/locale.php'); ?>
<?php
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE citta SET id_provincia=%s, nome_citta=%s WHERE id_citta=%s",
                       GetSQLValueString($_POST['id_provincia'], "int"),
                       GetSQLValueString($_POST['nome_citta'], "text"),
                       GetSQLValueString($_POST['id_citta'], "int"));

  mysql_select_db($database_locale, $locale);
  $Result1 = mysql_query($updateSQL, $locale) or die(mysql_error());

  $updateGoTo = "elenco_comuni.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
    $updateGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $updateGoTo));
}

mysql_select_db($database_locale, $locale);
$query_province = "SELECT * FROM province";
$province = mysql_query($query_province, $locale) or die(mysql_error());
$row_province = mysql_fetch_assoc($province);
$totalRows_province = mysql_num_rows($province);

$colname_comuni = "-1";
if (isset($_GET['id_citta'])) {
  $colname_comuni = (get_magic_quotes_gpc()) ? $_GET['id_citta'] : addslashes($_GET['id_citta']);
}
mysql_select_db($database_locale, $locale);
$query_comuni = sprintf("SELECT * FROM citta WHERE id_citta = %s", $colname_comuni);
$comuni = mysql_query($query_comuni, $locale) or die(mysql_error());
$row_comuni = mysql_fetch_assoc($comuni);
$totalRows_comuni = mysql_num_rows($comuni);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Untitled Document</title>
<link href="../style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="left">
    <tr valign="baseline">
      <td nowrap align="right">Id:</td>
      <td><?php echo $row_comuni['id_citta']; ?></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Provincia:</td>
      <td><select name="id_provincia">
        <?php 
do {  
?>
        <option value="<?php echo $row_province['id_provincia']?>" <?php if (!(strcmp($row_province['id_provincia'], $row_comuni['id_provincia']))) {echo "SELECTED";} ?>><?php echo $row_province['nome_provincia']?></option>
        <?php
} while ($row_province = mysql_fetch_assoc($province));
?>
      </select>
      </td>
    <tr>
    <tr valign="baseline">
      <td nowrap align="right">Nome Citta:</td>
      <td><input type="text" name="nome_citta" value="<?php echo $row_comuni['nome_citta']; ?>" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Update record"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_update" value="form1">
  <input type="hidden" name="id_citta" value="<?php echo $row_comuni['id_citta']; ?>">
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($province);

mysql_free_result($comuni);
?>
